// pages/page4/page4.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      opacity: 1,
      disabled: false,
      max: 30,
      min: 26,
      rule: 'up',
      items: [
        { name: 'up', value: '让巴西龟晒晒太阳', checked: 'ture' },
        { name: 'down', value: '让巴西龟回到阴凉处' },
      ]
    },
  
    radioChange: function (e) {
      //保存报警规则到当前页面的数据
      if (e.detail.value != "") {
        this.setData({
          rule: e.detail.value
        })
      }
      console.log(this.data.rule)
    },
  
    send: function () {
  
      var theBaiDuAPPkey = "hWQTNpXMexhxBWdXKY3vP7FHWibN54k0" 
      var district_id = "110108"  
      
      //调用百度天气API
      wx.request({
        url: `https://api.map.baidu.com/weather/v1/?district_id=${district_id}&data_type=all&ak=${theBaiDuAPPkey}`, //百度天气API
  
        success: (res) => {
          console.log(`APPKey: ${theBaiDuAPPkey}`, res.data)
          var tmp = res.data.result.now.temp
          
          //温度高于设置的门限值
          if (tmp > this.data.max) {
            if (this.data.rule == "up") {
              //规则为高于门限报警，于是报警
              wx.showModal({
                title: '',
                content: `温度太高啦，快让巴西龟回到阴凉处`
              })
            }
            //规则为低于门限报警，于是不报警
            else if (this.data.rule == "down") {
              wx.showModal({
                title: '',
                content: `做得很好哦，不要让巴西龟太热啦`
              })
            }
          }
          //温度低于设置的门限值
          else if (tmp < this.data.min) {
            //规则为高于门限报警，于是不报警
            if (this.data.rule == "up") {
              wx.showModal({
                title: '',
                content: `做得很好哦，巴西龟需要生活在温暖的地方`
              })
            }
            //规则为低于门限报警，于是报警
            else if (this.data.rule == "down") {
              wx.showModal({
                title: '',
                content: `温度太低啦，快让巴西龟晒晒太阳`
              })
            }
          }
          //温度适中，不报警
          else{
            wx.showModal({
              title: '',
              content: `当前温度${tmp}度,巴西龟生活得很好哦`
            })
          }
        },
        
  
        fail: function (res) {
          console.log("请求失败", res)
        }
      })
    },
  
    change: function (e) {
      //当有输入时激活发送按钮，无输入则禁用按钮
      if (e.detail.value != "") {
        this.setData({
          threshold: e.detail.value,
          opacity: 1,
          disabled: false,
        })
      } else {
        this.setData({
          threshold: 0,
          opacity: 1,
          disabled: false,
        })
      }
    }
  })
  
    